package com.eshore.norm.enumeration;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import org.apache.commons.lang3.StringUtils;

/**
 * 虚拟机状态枚举
 *
 * @author liangqb
 */
public enum CmdbVmStatusEnum {


    //虚机运行状态
    /**
     * 运行中
     */
    UNKNOWN(0),
    RUNNING(1),
    STOPPED(2),
    /**
     * 启动中
     */
    STARTING(3),
    /**
     * 停止中
     */
    STOPPING(4),
    /**
     * 重启中
     */
    REBOOT(5),

    /**
     * 删除中
     */
    DELETING(6);

    /**
     * 华三开机状态
     */
    public static final String H3C_RUNNING = "ACTIVE";
    /**
     * 华三关机状态
     */
    public static final String H3C_SHUTOFF = "SHUTOFF";

    @EnumValue
    @JsonValue
    private final int code;

    CmdbVmStatusEnum(Integer code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    @JsonCreator
    public static CmdbVmStatusEnum valOf(Integer code) {
        if (code == null) {
            return null;
        }

        for (CmdbVmStatusEnum value : values()) {
            if (value.code == code) {
                return value;
            }
        }
        return null;
    }

    /**
     * 状态转换方法
     *
     * @param status
     *         华三状态
     *
     * @return 转换后系统对应状态
     */
    public static CmdbVmStatusEnum h3cCovert(String status) {
        if (StringUtils.isBlank(status)) {
            return UNKNOWN;
        }
        if (status.equalsIgnoreCase(H3C_RUNNING)) {
            return RUNNING;
        } else if (status.equalsIgnoreCase(H3C_SHUTOFF)) {
            return STOPPED;
        } else {
            // TODO:(ZZY2020,2021/4/1 9:21,[待补充其他状态])
            return UNKNOWN;
        }
    }

    /**
     * 状态转换方法
     *
     * @param status
     *         华山状态
     *
     * @return 转换后系统对应状态
     */
    public static CmdbVmStatusEnum h3cHostVmCovert(String status) {
        if (StringUtils.isBlank(status)) {
            return UNKNOWN;
        }
        if (status.equalsIgnoreCase("running")) {
            return RUNNING;
        } else if (status.equalsIgnoreCase(H3C_SHUTOFF)) {
            return STOPPED;
        } else {
            // TODO:(ZZY2020,2021/4/1 9:21,[待补充其他状态])
            return UNKNOWN;
        }
    }

    /**
     * 华为云开机状态
     */
    public static final String MANAGEONE_ACTIVE = "active";
    /**
     * 华为云关机状态
     */
    public static final String MANAGEONE_STOPPED = "stopped";

    /**
     * 状态转换方法
     *
     * @param status
     *          华为虚机状态
     *
     * @return 转换后系统对应状态
     */
    public static CmdbVmStatusEnum manageOneCovert(String status) {
        if (StringUtils.isBlank(status)) {
            return UNKNOWN;
        }
        switch (status) {
            case MANAGEONE_ACTIVE:
                return RUNNING;
            case MANAGEONE_STOPPED:
                return STOPPED;
            default:
                return UNKNOWN;
        }
    }

    /**
     * 华山开机状态
     */
    public static final String CAS_RUNNING = "running";
    public static final Integer CAS_START = 2;
    /**
     * CAS关机状态
     */
    public static final String CAC_SHUTOFF = "shutOff";
    public static final Integer CAC_STOP = 3;

    /**
     * 状态转换方法
     *
     * @param status
     *          CAS虚机状态
     *
     * @return 转换后系统对应状态
     */
    public static CmdbVmStatusEnum CasCovert(String status) {
        if (StringUtils.isBlank(status)) {
            return UNKNOWN;
        }
        switch (status) {
            case CAS_RUNNING:
                return RUNNING;
            case CAC_SHUTOFF:
                return STOPPED;
            default:
                return UNKNOWN;
        }
    }

    public static CmdbVmStatusEnum CasCovert(Integer status) {
        if (status.equals(CAS_START)) {
            return RUNNING;
        } else if (status.equals(CAC_STOP)) {
            return STOPPED;
        } else {
            // TODO:([待补充其他状态])
            return UNKNOWN;
        }
    }

    /**
     * hcl开机状态
     */
    public static final String HCL_RUNNING = "running";
    /**
     * hcl关机状态
     */
    public static final String HCL_SHUTOFF = "stopped";

    public static CmdbVmStatusEnum hclCovert(String status) {
        if (StringUtils.isBlank(status)) {
            return UNKNOWN;
        }
        if (status.equalsIgnoreCase(HCL_RUNNING)) {
            return RUNNING;
        } else if (status.equalsIgnoreCase(HCL_SHUTOFF)) {
            return STOPPED;
        } else {
            // TODO:(ZZY2020,2021/4/1 9:21,[待补充其他状态])
            return UNKNOWN;
        }
    }

    /**
     * OpenStack开机状态
     */
    public static final String OPENSTACK_RUNNING = "active";
    /**
     * OpenStack关机状态
     */
    public static final String OPENSTACK_STOPPED = "stopped";
    public static final String OPENSTACK_ERROR = "error";

    /**
     * 状态转换方法
     *
     * @param status
     *         OpenStack状态
     *
     * @return 转换后系统对应状态
     */
    public static CmdbVmStatusEnum openstackCovert(String status) {
        if (StringUtils.isBlank(status)) {
            return UNKNOWN;
        }
        if (status.equalsIgnoreCase(OPENSTACK_RUNNING)) {
            return RUNNING;
        } else if (status.equalsIgnoreCase(OPENSTACK_STOPPED) || status.equalsIgnoreCase(OPENSTACK_ERROR)) {
            return STOPPED;
        } else {
            // TODO:(ZZY2020,2021/4/1 9:21,[待补充其他状态])
            return UNKNOWN;
        }
    }
}
